<?php

namespace App\Http\Controllers\Auth;

use App\Events\User\LoginedEvent;
use App\Models\User;
use Auth;
use Exception;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

/**
 * 处理用户登录逻辑控制器
 *
 * Date: 19/03/2018
 * @author George
 * @package App\Http\Controllers\Auth
 */
class LoginController extends Controller
{
    use AuthenticatesUsers;

	/**
	 * 处理用户登录逻辑
	 *
	 * Date: 19/03/2018
	 * @author George
	 * @param LoginRequest $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function login(LoginRequest $request)
	{
		// 验证用户输入凭证
		$this->validate($request, [
			'username' => 'required|string',
			'password' => 'required|string',
		], [
			'username.required' => '请输入用户名',
			'password.required' => '请输入密码',
		]);

		// 获取凭证
		$credentials = [
			$this->username() => $request->get('username'),
			'password' => $request->get('password')
		];
		// 判断是否是管理员账户登录
		$guard = $request->get('guard', 'user');
		try {
			if (! $token = $this->guard($guard)->attempt($credentials)) {
				return failed('认证失败，用户名或密码不正确', 401);
			}
		} catch (JWTException $exception) {
			return internalError();
		}

		/**
		 * @var User $user
		 */
		$user = Auth::guard($guard)->user();

		if ($guard === 'admin') {
			return success([
				'id' => $user->id,
				'name' => $user->name,
				'avatar' => $user->avatar,
				'access_token' => $token,
				'token_type' => 'Bearer',
				'expires_in' => config('jwt.ttl') * 60,
			]);
		}
		// 触发登陆事件
		event(new LoginedEvent($user));

		return success([
            'id' => $user->id,
            'name' => $user->name,
            'mobile' => $user->mobile,
            'birthday' => $user->birthday,
            'gender' => $user->gender,
            'identification' => $user->identification,
            'company_id' => $user->company_id,
            'project_id' => $user->project_id,
            'position_id' => $user->position_id,
            'avatar' => $user->avatar,
            'certificate' => $user->certificate,
            'manager' => $user->manager,
            'registration_id' => $user->registration_id,
			'personal' => $user->personal,
			'company' => $user->company,
			'access_token' => $token,
			'token_type' => 'Bearer',
			'expires_in' => config('jwt.ttl') * 60,
		]);
    }

	/**
	 * 处理用户注销逻辑
	 *
	 * Date: 19/03/2018
	 * @author George
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function logout()
	{
		try {
			$this->guard()->logout();
		} catch (Exception $exception) {
			return internalError();
		}
		return respond([], '注销成功');
    }

	/**
	 * 定义用于验证的用户的字段
	 *
	 * Date: 2018/1/25
	 * @author George
	 * @return string
	 */
	public function username()
	{
		return 'mobile';
	}

	/**
	 * 定义默认的Guard
	 *
	 * Date: 2018/1/25
	 * @author George
	 * @param $guard
	 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
	 */
	public function guard($guard = 'user')
	{
		return Auth::guard($guard);
	}
}
